<template>
    <div>
        <!-- 三级分类全局组件 -->
        <Category />
        <el-card style="margin: 10px 0;">
            <el-button type="primary" @click="" icon="Plus"
                :disabled="categoryStore.c3Id ? false : true">添加属性</el-button>
            <el-table border style="margin: 10px 0;" :data="attrArr">
                <el-table-column label="序号" align="center" type="index" width="80px"></el-table-column>
                <el-table-column label="属性名称" width="120px" prop="attrName"></el-table-column>
                <el-table-column label="属性值名称">
                    <template #="{ row, $index }">
                        <el-tag style="margin: 5px;" v-for="(item, index) in row.attrValueList" :key="item.id">{{
                            item.valueName }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120px">
                    <template #="{ row, $index }">
                        <el-button type="primary" size="small" @click="" icon="Edit"></el-button>
                        <el-button type="primary" size="small" @click="" icon="Delete"></el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
</template>

<script setup lang='ts' name="Attr">
import { watch, ref, onBeforeUnmount } from 'vue'
import { reqAttr } from '@/api/product/attr';
import type { AttrResponseData, Attr } from '@/api/product/attr/type';
import useCategoryStore from '@/store/modules/category';
let categoryStore = useCategoryStore();
let attrArr = ref<Attr[]>([]);
//监听仓库三级分类id的变化
watch(() => categoryStore.c3Id, () => {
    //保证三级分类得有才能发请求
    attrArr.value = []
    if (!categoryStore.c3Id) return;
    getAttr();
})
const getAttr = async () => {
    const { c1Id, c2Id, c3Id } = categoryStore
    let result: AttrResponseData = await reqAttr(c1Id, c2Id, c3Id)
    if (result.code == 200) {
        attrArr.value = result.data
    }
}
//组件销毁前，清孔仓库关于分类的数据
onBeforeUnmount(() => {
    categoryStore.$reset();
})
</script>

<style></style>